package com.example.registerdemoyd203lizch01.slice;

import com.example.registerdemoyd203lizch01.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MainAbilitySlice extends AbilitySlice {
    private TextField userName;
    private  TextField passWord;
    private Button btnRegister;
    private Button btnLogin;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initvie();
        btnRegister.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                myregister();
            }
        });
        btnLogin.setClickedListener(component -> myLogin());

    }

    private void myLogin() {
        String useName = userName.getName();
        String password = passWord.getText();
        if (useName.isEmpty()||password.isEmpty()){
            new ToastDialog(this).setText("用户名或密码不能为空！").show();
        }else{
            DatabaseHelper databaesHelper = new DatabaseHelper(this);
            Preferences preferences=databaesHelper.getPreferences("mydata");
            preferences.getString("name",useName);
            preferences.getString("pwd",password);
            if (useName.isEmpty()||password.isEmpty()){
                new ToastDialog(this).setText("数据库不存在").show();
            }else if (useName.equals(userName)&&password.equals(password)){
                new ToastDialog(this).setText("登录成功！").show();
            }else {
                new ToastDialog(this).setText("用户名或密码不正确").show();
            }
        }
    }

    private void myregister() {
        String useName = userName.getText();
        String password = passWord.getText();
        if (useName.isEmpty()||password.isEmpty()){
            new ToastDialog(this).setText("用户名或密码不能为空").show();
        }else{
            DatabaseHelper databaseHelper=new DatabaseHelper(this);
            ohos.data.preferences.Preferences preferences=databaseHelper.getPreferences("mydata");
            preferences.putString("name",useName);
            preferences.putString("pwd",password);
            preferences.flush();
            new ToastDialog(this).setText("注册成功").show();
        }
    }

    private void initvie() {
        //获取id自己打
        userName=(TextField)findComponentById(ResourceTable.Id_text_field1);
        passWord=(TextField)findComponentById(ResourceTable.Id_text_field2);
        btnRegister=(Button) findComponentById(ResourceTable.Id_button1);
        btnLogin=(Button) findComponentById(ResourceTable.Id_button2);



    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
